怎么在Android中获取手机的SIM卡信息 |
您所在的位置:网站首页 › 安卓 获取手机号码 › 怎么在Android中获取手机的SIM卡信息 |
怎么在Android中获取手机的SIM卡信息
发布时间:2021-04-15 17:42:22
来源:亿速云
阅读:476
作者:Leah
栏目:移动开发
今天就跟大家聊聊有关怎么在Android中获取手机的SIM卡信息,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。 Android 获取本机手机号(适用于双卡双待手机) 直接上代码: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; import android.telephony.CellInfo; import android.telephony.TelephonyManager; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private TextView tv2; // /////////////////////////////////// static String ISDOUBLE; static String SIMCARD; static String SIMCARD_1; static String SIMCARD_2; static boolean isDouble; // ////////////////////////////////// @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.text); tv2 = (TextView) findViewById(R.id.text2); tv2.setText("不知道哪个卡可用!"); getNumber(); } private void getNumber() { TelephonyManager tm = (TelephonyManager) this.getSystemService(this.TELEPHONY_SERVICE); String phoneNumber1 = tm.getLine1Number(); // String phoneNumber2 = tm.getGroupIdLevel1(); initIsDoubleTelephone(this); if (isDouble) { // tv.setText("这是双卡手机!"); tv.setText("本机号码是:" + " " + phoneNumber1 + " " + "这是双卡手机!"); } else { // tv.setText("这是单卡手机"); tv.setText("本机号码是:" + " " + phoneNumber1 + " " + "这是单卡手机"); } } public void initIsDoubleTelephone(Context context) { isDouble = true; Method method = null; Object result_0 = null; Object result_1 = null; TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try { // 只要在反射getSimStateGemini 这个函数时报了错就是单卡手机(这是我自己的经验,不一定全正确) method = TelephonyManager.class.getMethod("getSimStateGemini", new Class[] { int.class }); // 获取SIM卡1 result_0 = method.invoke(tm, new Object[] { new Integer(0) }); // 获取SIM卡2 result_1 = method.invoke(tm, new Object[] { new Integer(1) }); } catch (SecurityException e) { isDouble = false; e.printStackTrace(); // System.out.println("1_ISSINGLETELEPHONE:"+e.toString()); } catch (NoSuchMethodException e) { isDouble = false; e.printStackTrace(); // System.out.println("2_ISSINGLETELEPHONE:"+e.toString()); } catch (IllegalArgumentException e) { isDouble = false; e.printStackTrace(); } catch (IllegalAccessException e) { isDouble = false; e.printStackTrace(); } catch (InvocationTargetException e) { isDouble = false; e.printStackTrace(); } catch (Exception e) { isDouble = false; e.printStackTrace(); // System.out.println("3_ISSINGLETELEPHONE:"+e.toString()); } SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); Editor editor = sp.edit(); if (isDouble) { // 保存为双卡手机 editor.putBoolean(ISDOUBLE, true); // 保存双卡是否可用 // 如下判断哪个卡可用.双卡都可以用 if (result_0.toString().equals("5") && result_1.toString().equals("5")) { if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "0"); } editor.putBoolean(SIMCARD_1, true); editor.putBoolean(SIMCARD_2, true); tv2.setText("双卡可用"); } else if (!result_0.toString().equals("5") && result_1.toString().equals("5")) {// 卡二可用 if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "1"); } editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, true); tv2.setText("卡二可用"); } else if (result_0.toString().equals("5") && !result_1.toString().equals("5")) {// 卡一可用 if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "0"); } editor.putBoolean(SIMCARD_1, true); editor.putBoolean(SIMCARD_2, false); tv2.setText("卡一可用"); } else {// 两个卡都不可用(飞行模式会出现这种种情况) editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, false); tv2.setText("飞行模式"); } } else { // 保存为单卡手机 editor.putString(SIMCARD, "0"); editor.putBoolean(ISDOUBLE, false); } editor.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }当然不要忘记添加权限: 获取双卡双待手机SIM卡信息 使用反射遍历 TelephonyManager 中的方法,通过肉眼基本能找到获取双卡双待号码的方法,最后通过反射取到 SIM 卡信息。 // 遍历 TelephonyManager 里的方法 public void printTelephonyManagerMethodNamesForThisDevice() { TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Class telephonyClass; try { telephonyClass = Class.forName(telephony.getClass().getName()); Method[] methods = telephonyClass.getMethods(); for (int i = 0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |